<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>系统配置管理</title>
<script type="text/javascript">
	var $grid, $detailGrid, selectTableRow;//选中的行数据
	var editIndex = -1;
	$(function() {
		$grid = $("#grid").datagrid(
				$.extend({}, datagridOption, {
					url : "${ctx}/basic/config/dataGrid",
					columns : [ [
							{
								field : 'ck',
								checkbox : true
							},
							{
								field : 'configNo',
								title : '配置编号',
								width : 140,
								sortable : true
							},
							{
								field : 'configName',
								title : '配置名称',
								width : 140,
								sortable : true
							},
							{
								field : 'configValue',
								title : '配置值',
								width : 140,
								sortable : true
							},
							{
								field : 'remark',
								title : '备注',
								width : 140,
								sortable : true
							},
							{
								field : 'enable',
								title : '是否启用',
								width : 100,
								formatter : function(value, row, index) {
									return '<input class="easyui-switchbutton" ' + (value == 1 ? "checked" : "")
											+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
								}
							} ] ],
					onDblClickRow : function(index, rowData) {
						$.isFunction(fn_edit) && fn_edit.call(this, rowData);
					}
				}));

		$grid.datagrid('enableFilter', [ buildDGFilterYesOrNoFieldOptions($grid, 'enable') ]);
		$("#enableSB").switchbutton({
			onText : "是",
			offText : "否",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#enable").val(1);
				} else {
					$("#enable").val(2);
				}
			}
		});
		$("#add").click(function() {
			$("#fm").form("clear");
			$('#fm').form('load', {
				enable : 1
			});
			$("#enableSB").switchbutton('check');
			$("#dlg").dialog("open").dialog("setTitle", "新增系统配置");

			selectTableRow = null;
			//清除所有选中的行，解决切换不同代码表后还保留代码值中上次选中行bug
			$grid.datagrid("clearSelections");
			$detailGrid.datagrid("clearSelections");
			refreshDetail();//刷新明细
		});
		$("#edit").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (!rowData) {
				showInfo("您未选中行，无法编辑");
			} else {
				fn_edit(rowData);
			}
		});
		$("#delete").click(function() {
			multiDeleteDataGrid($grid, "${ctx}/basic/config/multiDelete");
		});
		$("#search").click(function() {
			$grid.datagrid('load', $("#tt").form("getData"));
		});
		$("#excelExport").click(function() {
			var url = "${ctx}/basic/config/excelExport?" + $.param($grid.datagrid('options').queryParams);
			if ($grid.datagrid("options").filterRules) {
				url += "&filterRules=" + JSON.stringify($grid.datagrid("options").filterRules);
			}
			window.parent.location.href = url;
		});

		//-----------------明细---------------------
		$detailGrid = $("#detailGrid").datagrid($.extend({}, datagridOption, {
			url : "${ctx}/basic/config/dataGrid",
			toolbar : "",
			columns : [ [ {
				field : 'ck',
				checkbox : true
			}, {
				field : 'configNo',
				title : '配置编号',
				width : 140,
				sortable : true,
				editor : "textbox"
			}, {
				field : 'configName',
				title : '配置名称',
				width : 140,
				sortable : true,
				editor : "textbox"
			}, {
				field : 'configValue',
				title : '配置值',
				width : 140,
				sortable : true,
				editor : "textbox"
			}, {
				field : 'remark',
				title : '备注',
				width : 140,
				sortable : true,
				editor : "textbox"
			}, {
				field : 'enable',
				title : '是否启用',
				width : 100,
				formatter : function(value, row, index) {
					return value == 1 ? "是" : "否";
				},
				editor : comboboxEditorOption
			} ] ],
			autoEditingEvent : "onDblClickRow",
			autoEditing : true, //该属性启用双击行时自定开启该行的编辑状态
			extEditing : false, //该属性启用行编辑状态的 ExtEditing 风格效果，该属性默认为 true。
			singleEditing : false,

			onBeginEdit : function(rowIndex, rowData) {
				var _onBeginEdit = $.fn.datagrid.extensions.onBeginEdit;
				if ($.isFunction(_onBeginEdit)) {
					_onBeginEdit.apply(this, arguments);
				}
				editIndex = rowIndex;
			}
		}));
		$("#detailAdd").click(function() {
			if (endEditing()) {
				$detailGrid.datagrid('appendRow', {
					enable : 1
				});
				editIndex = $detailGrid.datagrid('getRows').length - 1;
				$detailGrid.datagrid('selectRow', editIndex).datagrid('beginEdit', editIndex);
			}
		});
		$("#redo").click(function() {
			$detailGrid.datagrid("rejectChanges");
		});
		$("#save").click(function() {
			if (endEditing()) {
				$detailGrid.datagrid("endEditAll");
				$("#fm").form("submit", {
					url : "${ctx}/basic/config/update",
					onSubmit : function(param) {
						var inserted = $detailGrid.datagrid('getChanges', "inserted");
						var updated = $detailGrid.datagrid('getChanges', "updated");
						var deleted = $detailGrid.datagrid('getChanges', "deleted");
						$("#fm").form("load", {
							detailInserted : JSON.stringify(inserted),
							detailUpdated : JSON.stringify(updated),
							detailDeleted : JSON.stringify(deleted)
						});
					},
					success : function(data) {
						showInfoForCallBack(data);
						if (data.success) {
							$grid.datagrid("reload");
							$("#fm").form("load", data.obj);
							selectTableRow = data.obj;
							$detailGrid.datagrid("reload");
						}
					}
				});
			}
		});
		$("#detailDelete").click(function() {
			multiDeleteDataGrid($detailGrid, "${ctx}/basic/config/multiDelete");
		});
		$("#detailExcelExport").click(
				function() {
					var url = "${ctx}/basic/config/excelExport?search_EQ_id=" + (selectTableRow ? selectTableRow.id : "") + "&"
							+ $.param($detailGrid.datagrid('options').queryParams)
					if ($detailGrid.datagrid("options").filterRules) {
						url += "&filterRules=" + JSON.stringify($detailGrid.datagrid("options").filterRules);
					}
					window.parent.location.href = url;
				});
		$("#showAll").click(function() {
			selectTableRow = null;
			$detailGrid.datagrid("clearSelections");//清除所有选中的行，解决始终保留代码值中上次选中行bug
			$detailGrid.datagrid({
				url : "${ctx}/basic/config/dataGrid"
			});
		});
	});
	<shiro:hasPermission name="config:update">
	function fn_edit(rowData) {
		$("#dlg").dialog("open").dialog("setTitle", "编辑系统配置");
		$("#fm").form("clear");
		$("#fm").form("load", rowData);
		$("#enableSB").switchbutton(rowData.enable == 1 ? "check" : "uncheck");

		selectTableRow = rowData;
		//清除所有选中的行，解决切换不同代码表后还保留代码值中上次选中行bug
		$detailGrid.datagrid("clearSelections");
		refreshDetail(rowData);//刷新明细
	}
	</shiro:hasPermission>
	//----------------明细----------------------------
	function endEditing() {
		if (editIndex == -1) {
			return true
		}
		if ($detailGrid.datagrid('validateRow', editIndex)) {
			$detailGrid.datagrid('endEdit', editIndex);
			editIndex = -1;
			return true;
		} else {
			return false;
		}
	}
	//刷新Excel导入明细列表
	function refreshDetail(rowData) {
		$detailGrid.datagrid({
			url : "${ctx}/basic/config/dataGrid?search_EQ_id=" + (rowData ? rowData.id : "empty")
		});
	}
</script>
</head>

<body>
	<div data-options="region:'center',fit:true,border:false">
		<table id="grid">
		</table>
	</div>
	<div id="tt" class="row">
		<!-- <div class="col-md-4">
			<form class="form-grid-search form-inline">
				<div class="form-group">
					<input type="text" class="form-control" name="search_LIKE_configName" placeholder="系统配置名称" />
				</div>
				<button type="button" id="search" class="btn btn-primary">
					<span class="glyphicon glyphicon-search"></span> 查询
				</button>
			</form>
		</div> -->
		<div class="col-md-12">
			<shiro:hasPermission name="config:update">
				<button id="add" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-plus"></span> 新增
				</button>
				<button id="edit" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-pencil"></span> 编辑
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="config:delete">
				<button id="delete" type="button" class="btn btn-danger">
					<span class="glyphicon glyphicon-minus"></span> 删除
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="config:export">
				<button id="excelExport" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-save"></span> Excel导出
				</button>
			</shiro:hasPermission>
		</div>
	</div>
	<shiro:hasPermission name="config:update">
		<div id="dlg" class="easyui-dialog" style="width: 95%; height: 98%;"
			data-options="resizable:true,modal:true,toolbar:'#detailTT',closed:true,maximizable:true">
			<div class="easyui-layout" data-options="fit:true">
				<div data-options="region:'north',split:true" style="height: 110px;">
					<form id="fm" method="post">
						<input type="hidden" name="id" />
						<input type="hidden" name="detailInserted" />
						<input type="hidden" name="detailUpdated" />
						<input type="hidden" name="detailDeleted" />
						<table id="tbl">
							<tr>
								<td><label>系统配置编号:</label></td>
								<td><input name="configNo" class="easyui-validatebox" data-options="required: true"></td>
								<td><label>系统配置名称:</label></td>
								<td><input name="configName" class="easyui-validatebox" data-options="required: true"></td>
								<td><label>系统配置值:</label></td>
								<td><input name="configValue" class="easyui-validatebox" data-options="required: true"></td>
							</tr>
							<tr>
								<td><label>是否启用:</label></td>
								<td><input id="enable" name="enable" type="hidden"> <input id="enableSB"></td>
								<td><label>备注:</label></td>
								<td colspan="3"><input name="remark" class="easyui-textbox" data-options="multiline:true,width:185,height:40"></td>
							</tr>
						</table>

					</form>
				</div>
				<div data-options="region:'center'">
					<table id="detailGrid">
					</table>

				</div>
			</div>
		</div>
		<div id="detailTT">
			<shiro:hasPermission name="config:update">
				<button id="save" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-ok"></span> 保存
				</button>
				<button id="detailAdd" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-plus"></span> 添加一行
				</button>
				<button id="redo" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-remove-circle"></span> 取消
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="config:delete">
				<button id="detailDelete" type="button" class="btn btn-danger">
					<span class="glyphicon glyphicon-minus"></span> 删除
				</button>
			</shiro:hasPermission>
			<button id="showAll" type="button" class="btn btn-primary">
				<span class="glyphicon glyphicon-search"></span> 显示全部
			</button>
			<shiro:hasPermission name="config:export">
				<button id="detailExcelExport" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-save"></span> Excel导出
				</button>
			</shiro:hasPermission>
		</div>
	</shiro:hasPermission>
</body>
</html>
